package com.quasar.hpatient.module.comm_compile_step1;

import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.RadioThreeDialog;
import com.quasar.hpatient.module.comm_compile.CompileActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseFragment;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.Aerainfo;

/**
 * description: 个人信息
 * created by kalu on 2018/4/9 14:57
 */
public class CompileStep1Fragment extends BaseFragment<CompileStep1Presenter> implements CompileStep1View {

    @Override
    public int initView() {
        return R.layout.fragment_comm_compile_step1;
    }

    @Override
    public void initDataLocal() {

        TextView box7 = getView().findViewById(R.id.compile_one_box8);
        if (null != box7) {
            box7.setOnClickListener(v -> showRadio(box7, 7));
        }
        TextView box8 = getView().findViewById(R.id.compile_one_box9);
        if (null != box8) {
            box8.setOnClickListener(v -> showDate(box8));
        }
        TextView box9 = getView().findViewById(R.id.compile_one_box10);
        if (null != box9) {
            box9.setOnClickListener(v -> showAddress(box9));
        }

        EditText box0 = getView().findViewById(R.id.compile_one_box1);
        getPresenter().addTextWatcher(this, box0, 0);

        EditText box2 = getView().findViewById(R.id.compile_one_box3);
        getPresenter().addTextWatcher(this, box2, 2);

        EditText box3 = getView().findViewById(R.id.compile_one_box4);
        getPresenter().addTextWatcher(this, box3, 3);
        box3.setOnFocusChangeListener((v, hasFocus) -> {
            final String str = box3.getText().toString();
            if (hasFocus || TextUtils.isEmpty(str) || str.endsWith("cm")) return;
            box3.setText(str + "cm");
        });

        EditText box4 = getView().findViewById(R.id.compile_one_box5);
        getPresenter().addTextWatcher(this, box4, 4);
        box4.setOnFocusChangeListener((v, hasFocus) -> {
            final String str = box4.getText().toString();
            if (hasFocus || TextUtils.isEmpty(str) || str.endsWith("Kg")) return;
            box4.setText(str + "Kg");
        });

        EditText box5 = getView().findViewById(R.id.compile_one_box6);
        getPresenter().addTextWatcher(this, box5, 5);
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void showRadio(TextView view, int position) {

        final RadioDialog dialog = new RadioDialog(getActivity());
        dialog.setOnMethodChangeListener(method -> {
            updataMesssage(position, method);
            setText(view, method);
        });

        dialog.show();
        dialog.setList(Arrays.asList("男", "女"));
        dialog.setLoop(false);
        dialog.setTitle("选择性别");
    }

    @Override
    public void showDate(TextView view) {

        final DateDialog dialog = new DateDialog(getActivity());
        dialog.show();
        dialog.setTitles("选择出生日期");
        dialog.setOnDateChangeListener((year, month, day) -> {
            updataMesssage(8, year + "-" + month + "-" + day);
            setText(view, year + "-" + month + "-" + day);
        });
    }

    @Override
    public void showAddress(TextView view) {

        final List<String> list1 = new ArrayList<>();
        final List<String> list2 = new ArrayList<>();
        final List<String> list3 = new ArrayList<>();

        final RadioThreeDialog dialog = new RadioThreeDialog(getActivity());
        dialog.setOnDialogChangeListener((str1, str2, str3, iswheel) -> {
            // LogUtil.e("showAddress ==> ", str1 + ", " + str2 + ", " + str3 + ", iswheel = " + iswheel);

            if (!iswheel) {
                updataMesssage(9, str1 + File.separator + str2 + File.separator + str3);
                setText(view, str1 + str2 + str3);
            } else if (!TextUtils.isEmpty(str2)) {
                final String areaCode = DBManager.getInstance().getAreaCode(str2);
                final List<Aerainfo> prefectureList = DBManager.getInstance().getAreaList(areaCode);
                list3.clear();
                for (Aerainfo model : prefectureList) {
                    list3.add(model.getAeraInfoName());
                }
                dialog.setList(null, null, list3);
            } else if (!TextUtils.isEmpty(str1)) {
                final String areaCode = DBManager.getInstance().getAreaCode(str1);
                final List<Aerainfo> cityList = DBManager.getInstance().getAreaList(areaCode);
                list2.clear();
                for (Aerainfo model : cityList) {
                    list2.add(model.getAeraInfoName());
                }
                dialog.setList(null, list2, null);
            }

        });
        dialog.show();
        dialog.setTitle("选择地址");

        final List<Aerainfo> provinceList = DBManager.getInstance().getProvinceList();
        for (Aerainfo model : provinceList) {
            list1.add(model.getAeraInfoName());
        }
        dialog.setList(list1, null, null);
    }

    @Override
    public void updataMesssage(int position, String str) {

        final FragmentActivity activity = getActivity();
        if (null == activity || !(activity instanceof CompileActivity)) return;

        CompileActivity activity1 = (CompileActivity) activity;
        activity1.updateMessage(position, str);
    }

    @Override
    public boolean isNull() {

        final TextView box1 = getView().findViewById(R.id.compile_one_box1);
        final TextView box3 = getView().findViewById(R.id.compile_one_box3);
        final TextView box4 = getView().findViewById(R.id.compile_one_box4);
        final TextView box5 = getView().findViewById(R.id.compile_one_box5);
        final TextView box6 = getView().findViewById(R.id.compile_one_box6);
        final TextView box8 = getView().findViewById(R.id.compile_one_box8);
        final TextView box9 = getView().findViewById(R.id.compile_one_box9);
        final TextView box10 = getView().findViewById(R.id.compile_one_box10);

        return TextUtils.isEmpty(box1.getText().toString()) ||
                TextUtils.isEmpty(box3.getText().toString()) ||
                TextUtils.isEmpty(box4.getText().toString()) ||
                TextUtils.isEmpty(box5.getText().toString()) ||
                TextUtils.isEmpty(box6.getText().toString()) ||
                TextUtils.isEmpty(box8.getText().toString()) ||
                TextUtils.isEmpty(box9.getText().toString()) ||
                TextUtils.isEmpty(box10.getText().toString());
    }
}